package gla.centralhotel.dao;

import gla.centralhotel.dao.interfaces.ICountryDAO;
import gla.centralhotel.model.Country;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Rivo
 */
@Stateless
public class CountryDAO implements ICountryDAO {
    @PersistenceContext
    private EntityManager em;

    public Country create(Country country) {
        em.persist(country);
        return country;
    }

    public Country edit(Country country) {
        em.merge(country);
        return country;
    }

    public void remove(Country country) {
        em.remove(em.merge(country));
    }

    public Country find(Object id) {
        return em.find(gla.centralhotel.model.Country.class, id);
    }

    public List<Country> findAll() {
        return em.createQuery("select object(o) from Country as o").getResultList();
    }
    
    public Country findCountryByCodeAndName(String code, String name){
        String query = "select ctry from Country as ctry where ctry.code = :code and ctry.name = :name";
        Query q = em.createQuery(query);
        q.setParameter("code", code);
        q.setParameter("name", name);
        return (Country) q.getSingleResult();
    }
    
    public Country findCountryByCode(String code){
        Query q = em.createNamedQuery("Country.findByCode");
        q.setParameter("code", code);
        return (Country) q.getSingleResult();
    }
    
    public Country findCountryByName(String name){
        Query q = em.createNamedQuery("Country.findByName");
        q.setParameter("name", name);
        return (Country) q.getSingleResult();
    }

}
